package com.example.ktapplication.fragment

import android.os.Bundle
import com.bumptech.glide.Glide
import com.example.ktapplication.R
import com.example.ktapplication.base.BaseFragment
import com.example.ktapplication.bean.Media
import com.example.ktapplication.databinding.FragmentMusicPlayerBinding
import com.example.ktapplication.utils.LogHelper
import com.example.ktapplication.utils.event.Event
import com.example.ktapplication.utils.event.EventBusUtil
import com.example.ktapplication.utils.toJson
import com.google.firebase.crashlytics.buildtools.reloc.com.google.common.eventbus.Subscribe
import xyz.doikki.videocontroller.StandardVideoController
import xyz.doikki.videoplayer.player.BaseVideoView
import xyz.doikki.videoplayer.player.VideoView

class MusicPlayerFragment() : BaseFragment<FragmentMusicPlayerBinding>() {

    private var curMedia: Media? = null

    companion object {
        const val PLAYER_STATE_PAUSE = 0
        const val PLAYER_STATE_PLAY = 1
        const val PLAYER_TAB_INDEX = 1
    }

    override fun getViewBind(): FragmentMusicPlayerBinding {
        return FragmentMusicPlayerBinding.inflate(layoutInflater)
    }

    override fun isRegisterEvent(): Boolean {
        return true
    }

    override fun onPause() {
        super.onPause()
        pausePlayer()
    }

    override fun onStop() {
        super.onStop()
        pausePlayer()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        releasePlayer()
    }

    override fun onDate(savedInstanceState: Bundle?) {
    }

    override fun onViewBing() {
        binding.ivPlay.setOnClickListener {
            playClickAction()
        }
        binding.ivMenu.setOnClickListener {
            EventBusUtil.eventBus.post(Event.GetMusicMenu())
        }
        bindPlayerState()
    }

    private fun bindPlayerState() {
        binding.vvMusicPanel.apply {
            StandardVideoController(requireActivity()).apply {
                addOnStateChangeListener(object : BaseVideoView.OnStateChangeListener {
                    override fun onPlayerStateChanged(playerState: Int) {
                    }

                    override fun onPlayStateChanged(playState: Int) {
                        LogHelper.e("---ext onPlayStateChanged $playState")
                        when (playState) {
                            VideoView.STATE_IDLE,
                            VideoView.STATE_PAUSED,
                            VideoView.STATE_START_ABORT,
                            VideoView.STATE_ERROR ->
                                playState(PLAYER_STATE_PLAY)

                            else ->
                                playState(PLAYER_STATE_PAUSE)
                        }
                    }
                })
                setVideoController(this)
            }
        }
    }

    private fun initMusicInfo(music: Media) {
        var isPlaying = false
        curMedia?.let {
            isPlaying = it.playUrl == music.playUrl
        }
        if (isPlaying) {
            playClickAction()
            return
        }
        curMedia = music
        binding.tvTitle.text = music.title
        binding.tvUser.text = music.user
        Glide.with(this).load(music.playUrl).into(binding.ivIcon)
        playState(PLAYER_STATE_PLAY)
        playMusic(music.playUrl)
    }

    private fun playState(state: Int) {
        var res = R.mipmap.ic_play
        when (state) {
            PLAYER_STATE_PAUSE -> {
                res = R.mipmap.ic_pause
            }
        }
        Glide.with(this).load(res).into(binding.ivPlay)
    }

    private fun playMusic(playUrl: String) {
        binding.vvMusicPanel.apply {
            pause()
            release()
            setUrl(playUrl)
            start()
        }
    }

    @Subscribe
    fun onPlayMusic(event: Event.PlayMusic) { //音乐播放
        LogHelper.e("---ext onPlayMusic ${event.toJson()}")
        val media = event.media
        if (media == null) {
            pausePlayer()
        } else {
            initMusicInfo(media)
        }
    }

    @Subscribe
    fun onMusicMenu(event: Event.MusicMenu) { //菜单弹窗
        showToast(event.toJson())
    }

    @Subscribe
    fun onMainClickMenu(event: Event.MainTab) { //菜单按钮事件
        if (event.position != PLAYER_TAB_INDEX) {
            pausePlayer()
        }
    }

    private fun playClickAction() {
        binding.vvMusicPanel.apply {
            val isPlaying = isPlaying
            if (isPlaying) {
                pause()
            } else {
                resume()
            }
        }
    }

    private fun pausePlayer() {
        binding.vvMusicPanel.apply {
            if (isPlaying) {
                pause()
            }
        }
    }

    private fun releasePlayer() {
        binding.vvMusicPanel.release()
    }

}
